﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;
using System.Drawing.Imaging;
using System.Net;
using CarLoanSystem.Web.Comman;
using CarLoanSystem.DAL;
using CarLoanSystem.Entity;

namespace CarLoanSystemWeb.Page.EditProfile
{
    public partial class wuc_edit_profile_donsman : UserControlManageForm
    {
        public event EventHandler InfoMessege;
        public event EventHandler ErrorMessege;
        public event EventHandler ErrorMessegeName;
        private static CustomerItem cusItem;
        private static BondsmanItem bondsmanItem;
        private static string mode;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cusItem = Session["Member"] as CustomerItem;
                if (this.Request.QueryString["id"] != null)
                {
                    mode = "Edit";
                    bondsmanItem = BondsmanInfoDAL.LoadBondsman(Convert.ToInt32(this.Request.QueryString["id"].ToString()));
                    Session["Bondsman"] = bondsmanItem;
                    this.txtAddress.Text = bondsmanItem.BONDSMAN_ADDRESS;
                    this.txtAddressWork.Text = bondsmanItem.BONDSMAN_WORK_ADDRESS;
                    this.txtAge.Text = bondsmanItem.BONDSMAN_AGE.ToString();
                    this.txtJob.Text = bondsmanItem.BONDSMAN_JOB;
                    this.txtLastName.Text = bondsmanItem.BONDSMAN_LASTNAME;
                    this.txtName.Text = bondsmanItem.BONDSMAN_NAME;
                    this.txtPerId.Text = bondsmanItem.BONDSMAN_PERSONAL_ID;
                    this.txtSalary.Text = bondsmanItem.BONDSMAN_SARARY.ToString();
                    this.txtTel.Text = bondsmanItem.BONDSMAN_TEL;
                    this.txtTelWork.Text = bondsmanItem.BONDSMAN_WORK_TEL;
                    BlindDataTable();
                }
                else
                {
                    mode = "Add";
                    bondsmanItem = new BondsmanItem();
                    this.lblSararay1.Visible = false;
                    this.lblSararay2.Visible = false;
                    this.lblSararay3.Visible = false;
                    this.txtSalary.Visible = false;
                    this.btnAddDoc.Visible = false;
                }
            }
        }

        private void BlindDataTable()
        {
            this.gvListDoc.DataSource = BondsmanDocInfoDAL.LoadBondsmanDocAll(bondsmanItem.CUSTOMER_ID);
            this.gvListDoc.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!BondsmanInfoDAL.check(this.txtPerId.Text))
                {
                    bondsmanItem.BONDSMAN_ADDRESS = this.txtAddress.Text;
                    bondsmanItem.BONDSMAN_WORK_ADDRESS = this.txtAddressWork.Text;
                    bondsmanItem.BONDSMAN_AGE = Convert.ToInt32(this.txtAge.Text);
                    bondsmanItem.BONDSMAN_JOB = this.txtJob.Text;
                    bondsmanItem.BONDSMAN_LASTNAME = this.txtLastName.Text;
                    bondsmanItem.BONDSMAN_NAME = this.txtName.Text;
                    bondsmanItem.BONDSMAN_PERSONAL_ID = this.txtPerId.Text;
                    bondsmanItem.BONDSMAN_TEL = this.txtTel.Text;
                    bondsmanItem.BONDSMAN_WORK_TEL = this.txtTelWork.Text;
                    bondsmanItem.CUSTOMER_ID = cusItem.CUSTOMER_ID;
                    if (mode == "Add")
                    {
                        bondsmanItem.BONDSMAN_SARARY = 0;
                        BondsmanInfoDAL.Insert(bondsmanItem);
                    }
                    else
                    {
                        bondsmanItem.BONDSMAN_SARARY = Convert.ToInt32(this.txtSalary.Text);
                        BondsmanInfoDAL.Update(bondsmanItem);
                    }
                    if (InfoMessege != null)
                    {
                        InfoMessege(this, EventArgs.Empty);
                    }
                }
                else 
                {
                    if (ErrorMessegeName != null)
                    {
                        ErrorMessegeName(this, EventArgs.Empty);
                    }
                }
            }
            catch(Exception ex)
            {
                if (ErrorMessege != null)
                {
                    ErrorMessege(this, EventArgs.Empty);
                }
            }
        }

        protected void AddDoc_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditProfileDocDonsmanPage.aspx");
        }

        protected void gvListDoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListDoc.PageIndex = e.NewPageIndex;
            gvListDoc.DataBind();
        }

        protected void gvListDoc_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BondsmanDocInfoDAL.Delete(Convert.ToInt32(gvListDoc.DataKeys[e.RowIndex].Values[0].ToString()));
        }

        protected void gvListDoc_EditCommand(object sender, GridViewEditEventArgs e)
        {
            try
            {
                BondsmanDocItem item = BondsmanDocInfoDAL.LoadBondsmanDoc(Convert.ToInt32(gvListDoc.DataKeys[e.NewEditIndex].Values[0].ToString()));
                if (item != null)
                {
                    Response.AddHeader("Content-type", item.BONDSMAN_DOC_EXTENSION);
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + item.BONDSMAN_DOC_NAME + item.BONDSMAN_DOC_EXTENSION);
                    Response.BinaryWrite(item.BONDSMAN_DOC_FILE);
                    Response.Flush();
                    Response.End();
                }
                else
                {
                    DataTable dx = (DataTable)gvListDoc.DataSource;
                    Response.AddHeader("Content-type", dx.Rows[e.NewEditIndex]["BONDSMAN_DOC_EXTENSION"].ToString());
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + dx.Rows[e.NewEditIndex]["BONDSMAN_DOC_NAME"].ToString() + dx.Rows[e.NewEditIndex]["BONDSMAN_DOC_EXTENSION"].ToString());
                    Response.BinaryWrite((byte[])dx.Rows[e.NewEditIndex]["BONDSMAN_DOC_FILE"]);
                    Response.Flush();
                    Response.End();
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
    }
}